package com.gugee.ins.data.blogger.es.core;

import com.gugee.ins.common.dal.service.media.InsMediaDao;
import com.gugee.ins.common.dal.service.media.InsRelMediaTaggerUserDao;
import com.gugee.ins.common.dal.service.statistics.InsStBloggerRealFollowingDao;
import com.gugee.ins.common.model.es.blogger.InsBlogger;
import com.gugee.ins.common.model.mysql.blogger.InsBloggerMain;
import com.xiguaji.boot.toolkit.ThreadPoolUtils;
import com.xiguaji.boot.toolkit.elasticsearch.RestClientHelper;
import lombok.extern.slf4j.Slf4j;
import lombok.val;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import java.util.List;


/**
 * 粉丝增长率更新ES
 */
@Slf4j
@Service
public class BloggerRealFollowerToEsService extends EsBaseService{

    String threadPoolName = "BloggerRealFollowerToEs-Thread";

    @Autowired
    InsMediaDao insMediaDao;

    @Autowired
    InsStBloggerRealFollowingDao insStBloggerRealFollowingDao;

    @Autowired
    InsRelMediaTaggerUserDao insRelMediaTaggerUserDao;

    @PostConstruct
    public void init() {
        forkJoinPool = ThreadPoolUtils.forkJoinPool(bloggerToElasticsearchAppConfig.getBloggerRealFollowerToEsThreadSize(), threadPoolName);
        scheduleTemplate.attach(threadPoolName, forkJoinPool);
        workName = "BloggerRealFollowerToEs";
    }

    @Override
    protected InsBlogger structureEsBlogger(InsBloggerMain insBloggerMain) {
        InsBlogger insBloggerEs = new InsBlogger();
        val insStBloggerRealFollowing = insStBloggerRealFollowingDao.findByUid(insBloggerMain.getUid());
        insBloggerEs.setId(insBloggerMain.getId());
        insBloggerEs.setRealFollowerCount(insStBloggerRealFollowing.getRealFollowerCount());
        insBloggerEs.setRealFollowerRate(insStBloggerRealFollowing.getRealFollowerRate());
        insBloggerEs.setSuspiciousRate(insStBloggerRealFollowing.getSuspiciousRate());
        insBloggerEs.setMassiveFollowingRate(insStBloggerRealFollowing.getMassiveFollowingRate());
        return insBloggerEs;
    }

    @Override
    protected void toEsHandler(String esIndex, List<InsBlogger> insBloggers) {
        RestClientHelper.upsert(esIndex,insBloggers);
    }
}
